﻿#ifndef CONTROLDEVICE_H
#define CONTROLDEVICE_H

#include <QObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QMap>
#include <QList>
#include <QPair>
#include <QSet>
#include <QMultiMap>
#include <QTimer>

#include "./commons/pindata.h"

/*
 * 中间控制器
*/
class ControlDevice : public QObject
{
    Q_OBJECT
public:
    explicit ControlDevice(QObject *parent = nullptr);
    virtual ~ControlDevice();

    static ControlDevice * gestance();
    void setStartCompany(bool status);
    bool getStartCompany();
    void handlStopOutTime();
    void handlRunOutTime(int times);

signals:


public slots:
    void onSigPinsInfor(QMap<int ,int> pinInfors);
    //查询液位
    void onSigFindLevel();
    // 顺序出料
    void onSigSequentialDischarge( uchar cmds, QByteArray infors);
    //顺序出料完成
    void onSigSequentialSuccess();

    // 同时出料
    void onSigSameDischarge( uchar cmds, QByteArray infors);

    //同时出料完成
    void onSigSameSuccess();


    //清洗
    void onSigCleanIndex(uchar cmds,int indexs);

    //清洗完成
    void onSigCleanSuccess(int errors);

    //电磁阀
    void onSigSolenoidValve( uchar cmds, QByteArray infors);

    //电磁阀处理完成
    void onSigMixDeviceSuccess();

    //不支持指令
    void onSigNotsupported( uchar cmds,QByteArray infors);

    //设备信号
    void onSigDevices( uchar cmds);

    //查询设备状态
    void onSigFindRuns( uchar cmds);

    //关闭
    void onSigCloseCmds( );

    //上端发送的退出指令
    void onSigCloseValues( uchar cmds, QByteArray infors);

    //处理浆泵
    void onSigTimeTasks(int pin , int statues);

    //mcu重启
    void onMcuTimeout();

    //mcu重启
    void onMcuCleanTimeout();

    //测试
    void onTestTimeout();

    void onSigElectric(int userPin , int realPin , int status);
    void onSigWarterHandl();
    void handlSigElectric();

    void onUpdateTime();

    void onOutTime();
    void onHandlRunOutTime(int times);

    void onCleanoutTimerout();

    //运行时间
    void onCleanRunTimeout(int times);

private:

    static ControlDevice * m_controlDevice;
    bool m_isClean;
    bool m_isCleanStops;
    bool m_isCleanPause;
    bool m_isOut;
    int m_cleanErrors;


    bool m_isCleanPiPles;
    QMap<int ,int> m_ledStatus;
    QList<int> m_listPins;
    uint8_t m_cmds;
    uint16_t m_numberIndexs;
    uint16_t m_cleanIndexs;
    QSet<int> m_listTimeTasks;
    QMap<int ,int> m_listTimeTasksMap;
    QTimer * m_mcuTimer;

    QTimer * m_testTimer;
    QMap<int ,int> m_clenErrors;
    QTimer * m_mcuUpdateTimes;

    QTimer * m_outTimer;

    QTimer * m_cleanTimerout;
    int m_cleanLens;
    QTimer * m_mcuCleanTimer;

};

#endif // CONTROLDEVICE_H
